<%--
  -	Date: 2012-03-27
  -
  - Copyright (C) 2012 Jawa Software. All rights reserved.
  - 
  - 系统缓存管理页面。
--%>
<%@page import="org.jawa.core.cache.CacheFactory"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="org.jawa.core.cache.Cache"%>
<%@ page import="org.jawa.admin.util.ParamUtils"%>
<%@ page import="java.text.DecimalFormat"%>

<jsp:useBean id="webManager" class="org.jawa.admin.util.WebManager" />
<% webManager.init(request, response, session, application, out); %>
<%
	String path = request.getContextPath();
%>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>系统缓存</title>
		<meta name="pageID" content="1103" />
		<style type="text/css">
		 .row-sel{
		 	background: #F7F7F9;
		 }
		</style>
	</head>
	<% 
    // 获取参数
    boolean doClearCache = request.getParameter("clear") != null;
    int refresh = ParamUtils.getIntParameter(request, "refresh", -1);
    int[] cacheIDs = ParamUtils.getIntParameters(request, "cacheID", -1);

    // 得到缓存列表
    Cache[] caches = webManager.getCaches();

    // 清除选择的缓存
    if (doClearCache) {
        for (int cacheID : cacheIDs) {
        	caches[cacheID].clear();
        }
    }

    // 格式化缓存度量值
    DecimalFormat mbFormat = new DecimalFormat("#0.00");
    DecimalFormat percentFormat = new DecimalFormat("#0.0");
    percentFormat.setNegativePrefix("");
%>

<%  // 缓存变量
    double overallTotal = 0.0;
    double overallTotalUsed = 0.0;
    double memUsed;
    double totalMem;
    double freeMem;
    double usedMem;
    String hitPercent;
    long hits;
    long misses;
%>
	<body>
		<div class="row-fluid">
		<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">服务器</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					缓存管理
				</li>
			</ul>
		   <div class="page-header">
				<h3>
					缓存管理
				</h3>
			</div>
			<%  if (doClearCache) { %>
				<div class="alert alert-success fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<strong>缓存清除成功!</strong>
				</div>
			<%  } %>
			<form action="<%=path %>/admin/system-cache.jsp" method="post"
				name="cacheForm" class="">
				<table class="table table-striped table-bordered ">
					<thead>
						<tr>
							<th style="text-align: center;">
								<input type="checkbox" name="" value="" onclick="handleCBClick(this);">
							</th>
							<th>
								缓存名称
							</th>
							<th>
								最大空间
							</th>
							<th>
								当前空间
							</th>
							<th>
								使用
							</th>
							<th>
								效率
							</th>
						</tr>
					</thead>
					<tbody>
					<%  
				    for (int i=0; i<caches.length; i++) {
				        Cache cache = caches[i];
				        if (cache.getMaxCacheSize() != -1 && cache.getMaxCacheSize() != Integer.MAX_VALUE) {
				            overallTotal += (double)cache.getMaxCacheSize();
				        }
				        if (cache.getCacheSize() != -1 && cache.getCacheSize() != Integer.MAX_VALUE) {
				        	overallTotalUsed += (double)cache.getCacheSize();
				        }
				        memUsed = (double)cache.getCacheSize()/(1024*1024);
				        totalMem = (double)cache.getMaxCacheSize()/(1024*1024);
				        freeMem = 100 - 100*memUsed/totalMem;
				        usedMem = 100*memUsed/totalMem;
				        hits = cache.getCacheHits();
				        misses = cache.getCacheMisses();
				        boolean lowEffec = false;
				        if (hits + misses == 0) {
				            hitPercent = "N/A";
				        }
				        else {
				            double hitValue = 100*(double)hits/(hits+misses);
				            hitPercent = percentFormat.format(hitValue) + "%";
				            lowEffec = (hits > 500 && hitValue < 85.0 && freeMem < 20.0);
				        }
				  %>
						<tr>
							<td style="text-align: center;">
								<input type="checkbox" name="cacheID" value="<%= i %>"
									onclick="updateControls(this.form);toggleHighlight(this);;">
							</td>
							<td><i class="icon-file "></i> <%= cache.getName() %></td>
							<td >
								<% if (cache.getMaxCacheSize() != -1 && cache.getMaxCacheSize() != Integer.MAX_VALUE) { %>
								<%= mbFormat.format(totalMem) %>
								MB
								<% } else { %>
								无限制
								<% } %>
							</td>
							<td ><%= mbFormat.format(memUsed)%>
								MB
							</td>
							<td >
								<% if (cache.getMaxCacheSize() != -1 && cache.getMaxCacheSize() != Integer.MAX_VALUE) { %>
								<%= percentFormat.format(usedMem)%>%
								<% } else { %>
								N/A
								<% } %>
							</td>
							<td ><%= hitPercent%></td>
						</tr>
						<%  } %>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="2" style="text-align: center;">
								合计:
							</td>
							<td><%= mbFormat.format(overallTotal/(1024.0*1024.0)) %>
								MB
							</td>
							<td colspan="3"><%= mbFormat.format(overallTotalUsed/(1024.0*1024.0)) %>
								MB
							</td>
						</tr>
					</tfoot>
				</table>
				<div class="form-actions">
					<button id="clearBtn" name="clear" type="submit"
						class="btn btn-primary" data-toggle="button">
						清除缓存
					</button>
				</div>
			</form>
		</div>
		<script src="<%= path %>/scripts/bootstrap.js"></script>
		<script>
			var selected = false;
			var cbstate = '';
			function handleCBClick(el) {
				var theform = el.form;
				for ( var i = 0; i < theform.elements.length; i++) {
					var theel = theform.elements[i];
					if (theel.name == 'cacheID') {
						theel.checked = !selected;
						toggleHighlight(theel);
					}
				}
				el.checked = !selected;
				selected = !selected;
				updateControls(theform);
			}
			function setCBState(theform) {
				for ( var i = 0; i < theform.elements.length; i++) {
					var theel = theform.elements[i];
					if (theel.name == 'cacheID') {
						cbstate += theel.checked;
					}
				}
			}
			function clearCBs(theform) {
				for ( var i = 0; i < theform.elements.length; i++) {
					var theel = theform.elements[i];
					if (theel.name == 'cacheID') {
						theel.checked = false;
					}
				}
			}
			function updateControls(theform) {
				var currentState = '';
				for ( var i = 0; i < theform.elements.length; i++) {
					var theel = theform.elements[i];
					if (theel.name == 'cacheID') {
						currentState += theel.checked;
					}
				}
				if (currentState != cbstate) {
					document.getElementById("clearBtn").disabled = false;
				} else {
					document.getElementById("clearBtn").disabled = true;
				}
			}
			function toggleHighlight(el) {
				var r = null;
				if (el.parentNode && el.parentNode.parentNode) {
					r = el.parentNode.parentNode;
				} else if (el.parentElement && el.parentElement.parentElement) {
					r = el.parentElement.parentElement;
				}
				if (r) {
					if (el.checked) {
						r.className = "row-sel";
					} else {
						r.className = "";
					}
				}
			}
			clearCBs(document.cacheForm);
			setCBState(document.cacheForm);
		</script>
	</body>
</html>
